﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Touch2Like.SharedLibrary
{
    public sealed class Strings
    {
        public static string Coalesce(object value, string defaultValue)
        {
            if (value == null)
                return defaultValue;
            else
                return value.ToString();
        }

        public static string Coalesce(object value)
        {
            return Coalesce(value, "");
        }

        public static string CompressGuid(string source)
        {
            return source.Replace("-", "").Replace("{", "").Replace("}", "");
        }

        public static string LeftString(string source, int length)
        {
            if (source.Length > length)
                return source.Substring(0, length);
            else
                return source;
        }

        public static string RightString(string source, int length)
        {
            if (source.Length > length)
            {
                string result = source.Substring(source.Length - length);
                return result;
            }
            else
                return source;
        }

        public static string FormatCurrency(object amount)
        {
            return FormatCurrency(amount, "<font color='Red'>", "</font>");
        }

        public static string FormatCurrency(object amount, string negativePreText, string negativePostText)
        {
            decimal theAmount;
            if (amount == null || amount == DBNull.Value)
            {
                theAmount = 0;
                amount = 0;
            }
            else
                theAmount = Convert.ToDecimal(amount);
            string result = theAmount.ToString("c");

            if (Convert.ToDecimal(amount) < 0)
                result = negativePreText + result + negativePostText;

            return result;
        }

        public static string JSApostropheEncode(string source)
        {
            return source.Replace("'", "\\'");
        }

        public static string JSEncode(string source)
        {
            string result = source.Replace("'", "\\'").Replace("\"", "\\\"");
            return result;
        }

        public static string ReverseString(string s)
        {
            char[] arr = s.ToCharArray();
            Array.Reverse(arr);
            return new string(arr);
        }
    }
}
